#include <stdio.h>

int main()
{
#if 0
    // 查找，数据存不存在，下标是多少。5这个数据
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int findData = 5;
    int idx = -1;

    for(int i = 0; i < 10; i++)
    {
        if(arr[i] == findData)
        {
            idx = i;
        }
    }
    if(idx == -1)
    {
        printf("No Find!\n");
    }
    else{
        printf("idx = %d\n",idx);
    }
#endif

    // 折半查找。要求是：数组是有序为前提
    int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int findData = 15;
    int left = 0, right = 9, midIdx, idx = -1;

    while(left <= right)
    {
        midIdx = (left + right)/2;
        if(findData == arr[midIdx])
        {
            idx = midIdx;
            break;
        }
        else if(findData > arr[midIdx])
        {
            left = midIdx + 1;
        }
        else {
            right = midIdx -1;

        }
    }
    if(idx == -1)
    {
         printf("Find None.\n");
    }
    else
    {
       printf("idx = %d\n",idx);
    }
    return 0;
}
